package jmine.tec.report.impl.table;

import java.io.Serializable;

import jmine.tec.report.api.RendererParameters;
import jmine.tec.report.api.table.ReportCell;
import jmine.tec.report.api.table.ReportColumn;
import jmine.tec.report.impl.RendererParametersImpl;

/**
 * Implementação padrão de coluna de relatório
 * 
 * @author lundberg
 * @param <V> tipo de valor
 */
public class ReportColumnImpl<V> implements ReportColumn<V>, Serializable {

    private final String id;

    private CellValueResolver<V> resolver;

    private ReportCell header;

    private ReportCell footer;

    private String rendererType;

    private RendererParameters parameters;

    /**
     * Construtor
     * 
     * @param id id
     * @param resolver resolver
     */
    public ReportColumnImpl(String id, CellValueResolver<V> resolver) {
        this.id = id;
        this.parameters = new RendererParametersImpl();
        this.resolver = resolver;
    }

    /**
     * {@inheritDoc}
     */
    public ReportCell createCell(V rowValue) {
        return this.doCreateCell(this.resolver.resolveCellValue(rowValue));
    }

    /**
     * Instancia uma nova célula contendo o valor dado
     * 
     * @param value value
     * @return ReportCell
     */
    protected ReportCell doCreateCell(Object value) {
        ReportCell cell = new ReportCellImpl(value);
        cell.setRendererType(this.rendererType);
        cell.getParameters().setParent(this.parameters);
        return cell;
    }

    /**
     * {@inheritDoc}
     */
    public ReportCell getFooter() {
        return this.footer;
    }

    /**
     * {@inheritDoc}
     */
    public ReportCell getHeader() {
        return this.header;
    }

    /**
     * {@inheritDoc}
     */
    public String getId() {
        return this.id;
    }

    /**
     * {@inheritDoc}
     */
    public void setHeader(ReportCell header) {
        header.getParameters().setParent(this.parameters);
        this.header = header;
    }

    /**
     * {@inheritDoc}
     */
    public void setFooter(ReportCell footer) {
        footer.getParameters().setParent(this.parameters);
        this.footer = footer;
    }

    /**
     * {@inheritDoc}
     */
    public String getRendererType() {
        if (this.rendererType == null) {
            this.rendererType = "string";
        }
        return this.rendererType;
    }

    /**
     * {@inheritDoc}
     */
    public void setRendererType(String rendererType) {
        this.rendererType = rendererType;
    }

    /**
     * {@inheritDoc}
     */
    public RendererParameters getParameters() {
        return this.parameters;
    }

    /**
     * {@inheritDoc}
     */
    public CellValueResolver<V> getCellValueResolver() {
        return this.resolver;
    }
}
